package com.net.oil.user;

import android.os.Bundle;
import
androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.bean.ListDate;
import com.net.oil.user.adapter.RecommendRecyclerViewAdapter;
import com.net.oil.user.bean.RecommendBean;
import com.net.oil.user.contract.MyRecommendListContract;
import com.net.oil.user.presenter.MyRecommendListPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyRecommendListActivity extends BaseActivity<MyRecommendListPresenter> implements MyRecommendListContract.View{

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.recommend_recycler_view)
    RecyclerView recommendRecyclerView;

    private RecommendRecyclerViewAdapter adapter;

    private int page = 1;           //当前页
    private int rows = 20;          //分页大小
    private boolean haveMore = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_recommend_list);
        ButterKnife.bind(this);
        presenter = new MyRecommendListPresenter(this);
        initView();

        initData();
    }

    private void initData() {
        presenter.getRecommendList(page, rows);
    }

    private void initView() {
        toolbarTitle.setText(getString(R.string.my_recommend));

        adapter = new RecommendRecyclerViewAdapter(this);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        recommendRecyclerView.setLayoutManager(manager);
        recommendRecyclerView.setAdapter(adapter);

        recommendRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!recyclerView.canScrollVertically(1) && haveMore && page > 1) {
                    recyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            adapter.changeMoreStatus(Constant.LOADING_MORE);
                        }
                    });
                    presenter.getRecommendList(page, rows);
                }
            }
        });
    }

    @OnClick(R.id.toolbar_go_back)
    public void onViewClicked() {
        finish();
    }

    /**
     * 获取推荐人列表成功
     * @param recommendBeans 推荐人列表
     */
    @Override
    public void getRecommendListSuccess(ListDate<RecommendBean> recommendBeans) {
        if (recommendBeans != null){
            if (recommendBeans.getList() != null && recommendBeans.getList().size() > 0){
                if (page == 1){
                    adapter.updateData(recommendBeans.getList());
                }else if (page > 1){
                    adapter.loadMore(recommendBeans.getList());
                }
                adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                haveMore = true;
                page++;
            }else {
                adapter.changeMoreStatus(Constant.LOADING_NONE);
                haveMore = false;
            }
        }else {
            adapter.changeMoreStatus(Constant.LOADING_NONE);
            haveMore = false;
        }
    }

    /**
     * 获取推荐人列表失败
     */
    @Override
    public void getRecommendListFailed() {

    }
}
